package com.ruoyi.search.service.impl;


import com.ruoyi.DataManage.domain.ExaminationCourse;
import com.ruoyi.DataManage.domain.Resource;
import com.ruoyi.DataManage.domain.Subject_tree;
import com.ruoyi.DataManage.domain.University;
import com.ruoyi.DataManage.mapper.*;
import com.ruoyi.search.domain.Search;
import com.ruoyi.search.domain.SearchVO;
import com.ruoyi.search.service.ISearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 考研初试资源查询Service业务层处理
 *
 * @author CehnFneg
 * @date 2024-04-27
 */
@Service
public class SearchServiceImpl implements ISearchService {

    @Autowired
    private UniversityMapper universityMapper;
    @Autowired
    private Subject_treeMapper subject_treeMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private ExaminationCourseMapper examinationCourseMapper;
    @Autowired
    private ResourceMapper resourceMapper;

    @Override
    public List<SearchVO> getSearchList(Search search) {
        List<SearchVO> searchVOList = new ArrayList<>();

        //获取学校
        Long universityId = search.getUniversityId();
        University university = universityMapper.selectUniversityById(universityId);

        //获取专业
        Long majorId = search.getMajorId();
        Subject_tree major = subject_treeMapper.selectSubject_treeById(majorId);

        //获取课程
        Long courseId = search.getCourseId();

        //选中了某个课程，只列举该课程下的资源
        if(courseId != null){
            //获取资源列表
            List<Resource> singleResourceList = resourceMapper.getResourceListByCourseId(courseId);
            for (Resource resource : singleResourceList) {
                SearchVO searchVO = new SearchVO();
                //设置学校名称
                searchVO.setUniversityName(university.getName());
                //设置专业名称
                searchVO.setMajorName(major.getName());
                ////获取考试科目名称
                searchVO.setCourseName(examinationCourseMapper.selectExaminationCourseById(courseId).getName());
                //获取资源名称
                searchVO.setResourceName(resource.getName());
                //获取资源分类
                searchVO.setClassification(resource.getClassification());
                //获取资源内容
                searchVO.setContent(resource.getContent());
                //获取资源描述
                searchVO.setDescription(resource.getDescription());

                searchVOList.add(searchVO);
            }
        }else{
            //未选中某个课程的情况，全部列举
            //通识课程列表(去除数学）
            List<ExaminationCourse> CommonCourseList = examinationCourseMapper.getCommonCourseList();
            //该专业需要考数学的话
            if(haveMath(majorId)){
                ExaminationCourse math = examinationCourseMapper.selectExaminationCourseById(1L);
                //将数学科目加入通识课程列表中
                CommonCourseList.add(math);
            }
            //专业课列表
            List<ExaminationCourse> MajorCourseList = examinationCourseMapper.getMajorCourseList(majorId);
            //两个列表合并
            CommonCourseList.addAll(MajorCourseList);
            for (ExaminationCourse course : CommonCourseList) {

                List<Resource> resourceList = resourceMapper.getResourceListByCourseId(course.getId());
                for (Resource resource : resourceList) {
                    SearchVO searchVO = new SearchVO();
                    //设置学校名称
                    searchVO.setUniversityName(university.getName());
                    //设置专业名称
                    searchVO.setMajorName(major.getName());
                    //设置考试科目名称
                    searchVO.setCourseName(course.getName());
                    //获取资源名称
                    searchVO.setResourceName(resource.getName());
                    //获取资源分类
                    searchVO.setClassification(resource.getClassification());
                    //获取资源内容
                    searchVO.setContent(resource.getContent());
                    //获取资源描述
                    searchVO.setDescription(resource.getDescription());

                    searchVOList.add(searchVO);
                }

            }
        }
        return searchVOList;
    }

    public boolean haveMath(Long majorId){
        //经济学 工学 军事学 考数学
        //一、获取门类名称
        //1、获取学科Id
        Long subjectId =  subject_treeMapper.getParentIdByMajorId(majorId);
        //2、获取门类Id
        Long categoryId = subject_treeMapper.getCategoryIdBySubjectId(subjectId);
        //3、获取门类名称
        String categoryName = categoryMapper.selectCategoryById(categoryId).getName();
        //通识课考数学的
        if("经济学".equals(categoryName) || "工学".equals(categoryName) || "军事学".equals(categoryName)){
            return true;
        }
        return false;
    }

}
